//
//  YMDatePickView.m
//  XunmallManager
//
//  Created by Mac on 2017/2/10.
//  Copyright © 2017年 yunmei. All rights reserved.
//

#import "YMDatePickView.h"
#define TWW self.frame.size.width
#define TWH self.frame.size.height

#define TWRGB(r,g,b) [UIColor colorWithRed:r/255.0 green:g/255.0 blue:b/255.0 alpha:1]
#define BtnW 60
#define toolH 60
#define BJH 260

@interface YMDatePickView ()<UITableViewDelegate,UITableViewDataSource>{
    
    UIView *_BJView;                //一个view，工具栏和pickview都是添加到上面，便于管理
    NSMutableArray *_DataAry;       //数据源
    
}
@property (nonatomic,strong) NSArray * OneTitleArray;
@property (nonatomic,strong) UITableView *listTableView;
@property (copy, nonatomic) void (^sele)(NSIndexPath *dateString);

@end

@implementation YMDatePickView
-(NSArray *)OneTitleArray {
    if (!_OneTitleArray) {
        _OneTitleArray = [NSArray arrayWithObjects:@"wodezhanghu",@"yichuli",@"jiaoyijilu",nil];
    }
    return _OneTitleArray;
}
-(instancetype)initWithFrame:(CGRect)rect selectItemTitle:(NSString *)title{
    if (self = [super initWithFrame:rect]) {
        _DataAry = [NSMutableArray array];
        self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0];
        [UIView animateWithDuration:0.3 animations:^{
            self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.3];
        }];
            NSArray *arr = @[@"银行卡",@"支付宝",@"微信"];
            [_DataAry addObjectsFromArray:arr];
        //显示pickview和按钮最底下的view
        _BJView = [[UIView alloc] initWithFrame:CGRectMake(0, TWH, TWW, BJH)];
        [self addSubview:_BJView];

        UIView *tool = [[UIView alloc] initWithFrame:CGRectMake(0, 0, TWW, toolH)];
        tool.backgroundColor = TWRGB(237, 236, 234);
        [_BJView addSubview:tool];
        
        UILabel *titleLB = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, TWW, toolH)];
        titleLB.text = title;
        titleLB.backgroundColor = [UIColor whiteColor];
        titleLB.textAlignment = NSTextAlignmentCenter;
        [tool addSubview:titleLB];
    
        self.listTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, tool.bottom, _BJView.width, _BJView.frame.size.height-toolH) style:UITableViewStylePlain];
        self.listTableView.delegate =self;
        self.listTableView.dataSource = self;
        [_BJView addSubview:self.listTableView];
    }
    return self;
    
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 3;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"UITableViewCell"];
    if (!cell) {
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"UITableViewCell"];
        cell.selectionStyle=UITableViewCellSelectionStyleNone;//cell不被选中
    }

    UIImageView * imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:self.OneTitleArray[indexPath.row]]];
    UILabel * cacheLab = [UILabel labelWithSize:[UIFont systemFontOfSize:13] withColor:[UIColor blackColor] withText:_DataAry[indexPath.row]];
    [cell addSubview:imageView];
    [cell addSubview:cacheLab];
    [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(@(GTFixHeightFlaot(10)));
        make.left.equalTo(@(_BJView.width/2 - GTFixWidthFlaot(50)));
        make.bottom.equalTo(cell.mas_bottom).offset(GTFixHeightFlaot(-10));
        make.width.equalTo(@(GTFixWidthFlaot(30)));
    }];
    [cacheLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(@(GTFixHeightFlaot(10)));
        make.left.equalTo(imageView.mas_right).offset(GTFixWidthFlaot(10));
        make.bottom.equalTo(cell.mas_bottom).offset(GTFixHeightFlaot(-10));
        make.width.equalTo(@(GTFixWidthFlaot(60)));
    }];
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return GTFixHeightFlaot(45);
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (self.sele) {
    self.sele(indexPath);
    }
    __weak typeof(UIView*)blockview = _BJView;
    __weak typeof(self)blockself = self;
    __block int blockH = TWH;
    
    [UIView animateWithDuration:0.1 animations:^{
        CGRect bjf = blockview.frame;
        bjf.origin.y = blockH;
        blockview.frame = bjf;
        blockself.alpha = 0.1;
    } completion:^(BOOL finished) {
        [blockself removeFromSuperview];
    }];
}
-(void)showItemView:(void (^)(NSIndexPath *))selectStr{

    self.sele = selectStr;

    [[[UIApplication sharedApplication] keyWindow] addSubview:self];

    __weak typeof(UIView*)blockview = _BJView;
    __block int blockH = TWH;
    __block int bjH = BJH;

    [UIView animateWithDuration:0.3 animations:^{
        CGRect bjf = blockview.frame;
        bjf.origin.y = blockH-bjH;
        blockview.frame = bjf;
    }];

}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    CGPoint point = [[touches anyObject] locationInView:self];
    if (!CGRectContainsPoint(_BJView.frame, point)) {
        __weak typeof(UIView*)blockview = _BJView;
        __weak typeof(self)blockself = self;
        __block int blockH = TWH;
        
        [UIView animateWithDuration:0.3 animations:^{
            CGRect bjf = blockview.frame;
            bjf.origin.y = blockH;
            blockview.frame = bjf;
            blockself.alpha = 0.1;
        } completion:^(BOOL finished) {
            [blockself removeFromSuperview];
        }];
    }
    
}

@end
